package com.sharemarking.system.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.sharemarking.system.dto.Page;
import com.sharemarking.system.dto.orgVerify.OrgVerifyListDto;
import com.sharemarking.system.dto.receipt.ReceiptListDto;
import com.sharemarking.system.mapper.ReceiptMapper;
import com.sharemarking.wa.common.entity.OrgVerify;
import com.sharemarking.wa.common.entity.Receipt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Controller
@RequestMapping("system/receiptManagement")
public class ReceiptManagementController {
    @Autowired
    private HttpSession session;
    @Autowired
    private ReceiptMapper rm;
    /**
     * 列表页面
     * @param
     * @param model
     */
    @GetMapping("index")
    @PreAuthorize("hasPermission('base','all')")
    public String index(Receipt obj, Integer page, Model model, Integer isSession){
        int size=10;
        if(page==null) {
            page=0;
        }
        // session 参数回显
        if (isSession != null) {
            // 如果isSession为1，代表要从session取条件参数，一般应用在某操作之后要保存查询参数
            obj = session.getAttribute("query") == null ? obj : (Receipt) session.getAttribute("query");
            size = session.getAttribute("size") == null ? size : (int) session.getAttribute("size");
            page = session.getAttribute("page") == null ? page : (int) session.getAttribute("page");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("pageIndex",page*size);
        map.put("pageSize",size);

        if(!StringUtils.isEmpty(obj.getState())) {
            map.put("state",obj.getState());
        }

        List<ReceiptListDto> list=rm.getReceiptList(map);
        int total = rm.getReceiptListTotal(map);
        model.addAttribute("page", new Page<>(page, size, total, list));
        model.addAttribute("query",obj);
        // session 参数保存
        session.setAttribute("query",obj);
        session.setAttribute("size", size);
        session.setAttribute("page", page);
        return "receipt/receiptIndex";
    }



    /**
     * 审核
     * @param obj
     * @param model
     * @return
     */
    @GetMapping("open")
    @PreAuthorize("hasPermission('base','all')")
    @Transactional
    public String open(int id, Model model, RedirectAttributes redirectAttributes){
        Receipt receipt = new Receipt();
        receipt.setId(id);
        receipt.setState(1);
        rm.updateById(receipt);

        List<Integer> list = rm.getPayRecordId(id);
        for (Integer i : list){
            rm.updatePayRecordState(i);
        }
        redirectAttributes.addFlashAttribute("msg","操作成功！");
        return "redirect:index?isSession=1";
    }




}
